package com.ymatou.iapi.couponactivity.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponactivity.parameter.ReceiveCouponBean;
import com.ymatou.iapi.couponactivity.service.ReceiveCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponActivityService;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponActivity;
import com.ymttest.database.model.YmtCouponActivityBatch;
import com.ymttest.database.model.YmtCouponActivityNewUserBound;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponActivityWapper;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 领取优惠券用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ReceiveCoupon {
	private static ReceiveCouponBean receivecouponBean;
	private static ReceiveCouponCall receivecouponCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("领取优惠券用例");
	}

	@Before
	public void caseUp() {
		receivecouponBean = new ReceiveCouponBean();
		receivecouponCall = new ReceiveCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(receivecouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_001() {
		Logger.start(true, "反例-活动不存在");
		try {
			receivecouponBean.setActivityCode(1888);
			receivecouponBean.setUserId(123456);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();

			Logger.verifyEquals("10001", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("活动不存在", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}      

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_002() {
		Logger.start(true, "反例-活动已失效");
		try {
			
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("unable").get(0);
			
			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(123456);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();
			
			Logger.verifyEquals("10002", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("活动已失效", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_003() {
		Logger.start(true, "反例-活动未开始");
		try {

			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("notstart").get(0);
			
			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(123456);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();
			
			Logger.verifyEquals("10003", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("活动未开始", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_004() {
		Logger.start(true, "反例-活动已过期");
		try {
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("end").get(0);
			
			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(123456);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();

			Logger.verifyEquals("10004", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("活动已过期", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_005() {
		Logger.start(true, "反例-已经领取过");
		try {
			receivecouponBean = new CouponActivityService().setReceiveCouponBean();
			receivecouponCall = new CouponActivityService().ReceiveCoupon(receivecouponBean);
			
			receivecouponBean.setActivityCode(receivecouponBean.getActivityCode());
			receivecouponBean.setUserId(receivecouponBean.getUserId());
			receivecouponBean.setMobile(receivecouponBean.getMobile());
			receivecouponBean.setDeviceId(receivecouponBean.getMobile());
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();
			
			Logger.verifyEquals("11003", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("已经领取过", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReceiveCoupon_006() {
		Logger.start(true, "正例-领取优惠券成功");
		try {
			
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("common").get(0);
			
			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper().selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			String mobilePhone = TestDataManager.getRandomNum(11);	
			String deviceNumber = "Dev"+TestDataManager.getRandomNum(11);	
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));

			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(userId);
			receivecouponBean.setMobile(mobilePhone);
			receivecouponBean.setDeviceId(deviceNumber);

			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();			

			// 验证返回数据
			Logger.verifyEquals("0", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("操作成功！", receivecouponCall.getMessage(), "验证返回Message");
			
			// 验证数据库落地
			// YmtCouponActivityNewUserBound表
			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper().selectCouponActivityNewUserBoundByMobilePhoneAndActivityCode(mobilePhone, activity.getActivityCode());
			
			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");
			
			if(bounds.size()==1)
			{
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				//Logger.verifyEquals(1, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");
				Logger.verifyEquals(userId, Integer.parseInt(bound.getUserId()), "验证数据库表Ymt_CouponActivityNewUserBound中字段UserId");
				Logger.verifyEquals(deviceNumber, bound.getDeviceNumber(), "验证数据库表Ymt_CouponActivityNewUserBound中字段DeviceNumber");
			
				//已发放
				Logger.verifyEquals(2, bound.getCouponSendStatus().intValue(), "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

				for(int i=0;i<settingIdlist.size();i++)
				{
					YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(settingIdlist.get(i)).get(0);
					Logger.verifyEquals(1, setting.getiReceiveCount()-receiveCountlist.get(i), "验证返回Ymt_CouponSetting表iSettingId="+settingIdlist.get(i)+"的记录中iReceiveCount多了1");
				}
				
				for (YmtCouponActivityBatch activityBatch : batchs) {
					YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
					
					List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batch.getiBatchId(), userId);
					Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound生成一条记录");

					List<YmtCoupon> coupons = new YmtCouponWapper().selectBysCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode());
					Logger.verifyEquals(1, coupons.size(), "验证返回YmtCoupon生成一条记录");
					
				}
			}
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReceiveCoupon_007() {
		Logger.start(false, "反例-请传入用户ID");
		try {
			receivecouponBean.setActivityCode(1888);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();

			Logger.verifyEquals("2", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("请传入用户ID", receivecouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReceiveCoupon_008() {
		Logger.start(false, "反例-请传入活动编号");
		try {

			receivecouponBean.setUserId(123456);
			receivecouponBean.setMobile("18612345678");
			receivecouponBean.setDeviceId("Dev123456789");
			
			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();

			Logger.verifyEquals("2", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("请传入活动编号", receivecouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_ReceiveCoupon_009() {
		Logger.start(true, "反例-JIRACONN-601用户没有参与过活动，但在其它地方领取过活动优惠券，系统返回用户超出批次领取限制");
		try {

			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("common").get(0);
			
			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper().selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			String mobilePhone = TestDataManager.getRandomNum(11);	
			String deviceNumber = "Dev"+TestDataManager.getRandomNum(11);	
			// int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				new CouponServiceCallService().receiveCouponInfo(batchCode, userId);
			}

			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(userId);
			receivecouponBean.setMobile(mobilePhone);
			receivecouponBean.setDeviceId(deviceNumber);

			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();			

			// 验证返回数据
			Logger.verifyEquals("11003", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("已经领取过", receivecouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_ReceiveCoupon_010() {
		Logger.start(true, "反例-用户存在风险-需要把手机号入风控黑名单,然后ignore取消");
		try {
			
			YmtCouponActivity activity = new YmtCouponActivityWapper().selectCouponActivityByActivityChannel("common").get(0);
			
			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper().selectYmtCouponActivityBatchByActivityCode(activity.getActivityCode());
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();
			
			String batchCode="";
			
			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length-1];
				
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}
			
			String deviceNumber = "Dev"+TestDataManager.getRandomNum(11);	
			int userId = 30000000+Integer.parseInt(TestDataManager.getRandomNum(7));

			receivecouponBean.setActivityCode(activity.getActivityCode());
			receivecouponBean.setUserId(userId);
			receivecouponBean.setMobile("18516029204");
			receivecouponBean.setDeviceId(deviceNumber);

			receivecouponCall.setData(receivecouponBean);
			receivecouponCall.callService();			

			// 验证返回数据
			Logger.verifyEquals("12001", receivecouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("用户存在风险", receivecouponCall.getMessage(), "验证返回Message");		

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}